/*
 * @Author: 烟浮
 * @Date: 2022-06-18 15:22:40
 * @LastEditTime: 2023-03-22 11:54:06
 * @LastEditors: 锦墨涵秋 275740441@qq.com
 * @FilePath: /ViteSsoIndex/ViteSsoIndex/src/service/index.ts
 */
// SERVICE 统一出口
import SlRequest from './request'
import { BASE_URL, TIME_OUT } from './request/config'
import localCache from '@/utils/cache'

const slRequest = new SlRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestInterceptor: (config): any => {
      if (!config?.headers) {
        throw new Error(
          `Expected 'config' and 'config.headers' not to be undefined`
        )
      }
      // 携带 token 的拦截
      const token = localCache.getCache('token')
      config.headers.Authorization = `bearer ${token}`
      return config
    },
    requestInterceptorCatch: (err) => {
      // console.log('请求失败的拦截')
      return err
    },
    responseInterceptor: (res) => {
      // console.log('响应成功的拦截')
      return res
    },
    responseInterceptorCatch(error) {
      // console.log('响应失败的拦截')
      return error
    }
  }
})

export default slRequest
